/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.staf.NetworkReportGenerator.V2;

import ru.staf.NetworkReportGenerator.V2.Panasonic.*;

import java.util.Set;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import java.net.MalformedURLException;

/**
 *
 * @author aezhov
 */
public class PanasonicFactory implements DeviceFactory {

    public LeafDevice getConcreteNode(String mac) {
        Set<String> ips = null;
        NmapOutputParser nmap = ConfigParser.nmap;

        if (nmap != null) {
            ips = nmap.getIpsForMac(mac);
        }

        if (ips == null || ips.size() == 0) {
            return new Unknown(mac, "");
        }

        System.out.println("PanasonicFactory.getConcreteNode: " + mac);

        return detectDeviceType(ips, mac);
    }


    LeafDevice detectDeviceType(Set<String> ips, String mac) {
        for (String ip : ips) {
            if (ConfigParser.dontProbeDevices) {
                LeafDevice unknown = new LeafDevice(mac, ip);
                unknown.setVendor("Panasonic");
                unknown.setModel("");
                return unknown;
            }

            String url = "http://" + ip;
            String html = HtmlUtil.readURL(url);

            if (html != null) {
                System.out.println("  HTML: " + html);
                // Detecting camera
                int x = html.indexOf("networkcamera");
                if (x > 0) {
                    System.out.println("Looks like network cam");
                    return new Camera(html, ip, mac);
                }

                // Detecting recorder WJND
                x = html.indexOf("wjnd");
                if (x > 0) {
                    System.out.println("Looks lile WJ-ND recorder");
                    return new RecorderWJND(html, ip, mac);
                }

                // Detecting decoder
                x = html.indexOf("framesrc=\"main.html\"");
                if (x > 0) {
                    System.out.println("Looks like network decoder");
                    return new Decoder(html, ip, mac);
                }

                // No markers of concrete device found. Creating anonymous
                // Panasonic node
                LeafDevice unknown = new LeafDevice(mac, ip);
                unknown.setVendor("Panasonic");
                unknown.setModel("");
                return unknown;
            }
        }

        return null;
    }
}
